import com.android.build.api.dsl.ApplicationExtension
import com.android.build.gradle.internal.api.ApkVariantOutputImpl
import com.android.build.gradle.internal.dsl.BaseAppModuleExtension
import com.matuw.android.plugin.ProjectExtensions
import org.gradle.api.JavaVersion
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.kotlin.dsl.configure
import org.gradle.kotlin.dsl.dependencies
import org.gradle.kotlin.dsl.extra
import org.gradle.kotlin.dsl.support.uppercaseFirstChar
import com.matuw.android.plugin.configureKotlinAndroid

class AndroidApplicationConventionPlugin : Plugin<Project> {
    override fun apply(target: Project) {
        with(target) {
            with(pluginManager) {
                apply("com.android.application")
                apply("org.jetbrains.kotlin.android")
            }

            extensions.configure<ApplicationExtension> {
                compileSdk = ProjectExtensions.COMPILE_SDK_VERSION

                defaultConfig {
                    minSdk = ProjectExtensions.MIN_SDK_VERSION
                    targetSdk = ProjectExtensions.TARGET_SDK_VERSION

                    versionCode = ProjectExtensions.VERSION_CODE
                    versionName = ProjectExtensions.VERSION_NAME
                }

                compileOptions {
                    sourceCompatibility = JavaVersion.VERSION_1_8
                    targetCompatibility = JavaVersion.VERSION_1_8
                }

                @Suppress("UnstableApiUsage")
                testOptions.animationsDisabled = true

                buildTypes {
                    release {
                        isMinifyEnabled = true
                        proguardFiles(
                            getDefaultProguardFile("proguard-android.txt"),
                            "proguard-rules.pro",
                        )
                    }
                }

                if (this is BaseAppModuleExtension) {
                    applicationVariants.configureEach {
                        outputs.all {
                            if (this is ApkVariantOutputImpl) {
                                val fileName = outputFileName
                                val dotIndex = fileName.lastIndexOf(".")
                                val suffix =
                                    if (dotIndex > 0) fileName.substring(dotIndex) else ".apk"
                                val type = buildType.name.takeIf { it.equals("debug", true) }
                                    ?.uppercaseFirstChar()
                                    ?: ""

                                val customTag = project.extra.takeIf { it.has("buildCustomTag") }
                                    ?.get("buildCustomTag")
                                    ?.toString()
                                    ?: ""

                                outputFileName =
                                    "${applicationId}_${defaultConfig.versionName}_${flavorName}$type${customTag}$suffix"
                            }
                        }
                    }
                }

                configureKotlinAndroid(this)
            }

            dependencies {
                val args = mapOf("include" to arrayOf("*.jar"), "dir" to "libs")
                add("implementation", fileTree(args))
            }
        }
    }

}
